﻿Imports System.ComponentModel
Imports System.Runtime.Versioning

<SupportedOSPlatform("windows")>
<SupportedOSPlatform("linux")>
Public Class SerialProxy

    <Description("监听的端口")>
    Public Property NetPort As UInteger

    <Description("打开的串口")>
    Public Property ComPort As String

    <Description("波特率")>
    Public Property BaudRate As UInteger

    Private eth As WebServer = New WebServer()
    Private serial As SerialClient

    Public Sub New(port As UInteger, com As String, Optional baudrate As UInteger = 9600)
        NetPort = port
        ComPort = com
        Me.BaudRate = baudrate
        serial = New SerialClient(com, baudrate)
        AddHandler serial.OnMessage, Sub(b)
                                         eth.Write(b)
                                     End Sub
        AddHandler eth.OnMessage, Sub(b)
                                      serial.Write(b)
                                  End Sub
    End Sub

    Public Sub Begin()
        Task.Factory.StartNew(Sub()
                                  eth.Listen(NetPort)
                              End Sub, TaskCreationOptions.LongRunning)
        Task.Factory.StartNew(Sub()
                                  serial.Begin()
                              End Sub, TaskCreationOptions.LongRunning)
    End Sub

End Class
